package com.googlecode.mgwt.examples.uibinder.client;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.place.shared.Place;
import com.googlecode.mgwt.examples.uibinder.client.activities.ExampleActivity;
import com.googlecode.mgwt.examples.uibinder.client.activities.ExampleDisplay;
import com.googlecode.mgwt.examples.uibinder.client.activities.ExampleSourceActivity;
import com.googlecode.mgwt.examples.uibinder.client.places.ExamplePlace;
import com.googlecode.mgwt.examples.uibinder.client.places.ExamplePlace.ID;
import com.googlecode.mgwt.examples.uibinder.client.places.ExampleSourcePlace;

public class TabletMainActivityMapper implements ActivityMapper {

	private final ClientFactory clientFactory;
	private DisplayMapper displayMapper;

	public TabletMainActivityMapper(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
		displayMapper = new DisplayMapper(this.clientFactory);

	}

	@Override
	public Activity getActivity(Place place) {

		if (place instanceof ExampleSourcePlace) {
			ExampleSourcePlace exampleSourcePlace = (ExampleSourcePlace) place;
			return new ExampleSourceActivity(clientFactory, exampleSourcePlace.getId());
		}

		ID id = ID.UNKNOWN;
		ExampleDisplay display = getDisplay(place);
		if (display == null)
			return null;
		if (place instanceof ExamplePlace) {
			ExamplePlace examplePlace = (ExamplePlace) place;
			id = examplePlace.getId();

		}

		return new ExampleActivity(clientFactory, display, id);

	}

	private ExampleDisplay getDisplay(Place place) {
		if (place instanceof ExamplePlace) {
			return displayMapper.getDisplay((ExamplePlace) place);
		}
		return null;
	}

}
